﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Configuration;
using System.Security.Principal;
using System.Web;
using Service.Core;
using Service.Core.Interface;
using Service.Core.Result;
using Tools;
using Web.BML.AnalysisData;
using Web.BML.CoreData;
using Web.Models.AnalysisData;
using Web.Models.CoreData;
using Web.ViewModel.CoreData;

namespace Web.Service
{
    public  class ShipService : BaseService
    {

        private const String ShipxyUrl = "api.shipxy.com/apicall/index";




        private ShipOp shipOp = ShipOp.GetInstance();

        private static ShipService instance;

        private ShipService()
        {

        }

        public static ShipService GetInstance()
        {
            if (instance == null)
            {
                instance = new ShipService();
            }
            return instance;
        }

        /// <summary>
        /// 获取用户的所有船
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<ShipModel> GetUserShipModels(String userId)
        {
            return shipOp.QueryShips(userId).Select(m=>new ShipModel().ConvertModel(m)).ToList();
        }

        /// <summary>
        /// 获取用户的所有船
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<ShipModel> GetUserShipsModels(String account)
        {
            User user = UserOp.GetInstance().QueryUser(account);
            if(user==null)
            {
                return new List<ShipModel>();
            }else
            {
                return GetUserShipModels(user.Id);
            }
        }
        



        /// <summary>
        /// 查询船讯网的MMSI
        /// </summary>
        /// <param name="mmsis"></param>
        /// <returns></returns>
        public ShipXYAPIModel QueryShipXYMMSI(string mmsis)
        {
            Dictionary<string, object> dict = new Dictionary<string, object>()
            {
                {"v", "1"},
                {"k", "7154922AE9AEBD5224F5B351F82E4C0A"},
                {"enc", "1"},
                {"cmd", "2003"},
                { "id" ,mmsis}
            };
            return WebHelper.ConvertObject<ShipXYAPIModel>(WebHelper.Get(ShipxyUrl, false, dict, null, null, null));
        }

        /// <summary>
        /// 查询船舶详细信息
        /// </summary>
        /// <param name="mmsis"></param>
        /// <returns></returns>
        public IServiceResult QueryShipDetail(String mmsis)
        {
            if (string.IsNullOrEmpty(mmsis))
            {
                return Result(ResultType.ParamIsNull, "MMSI输入为空");
            }
            ShipXYAPIModel xyApimodel = QueryShipXYMMSI(mmsis);
            if (xyApimodel.status != "0")
            {
                return Result(ResultType.ParamIsError, xyApimodel.msg);
            }
            String[] mmsiArr = mmsis.Split(new string[] {","}, StringSplitOptions.RemoveEmptyEntries);
            List<ShipInfoListModel> listModel=new List<ShipInfoListModel>();
            foreach (string mmsi in mmsiArr)
            {
                ShipXYModel model = xyApimodel.data.FirstOrDefault(m => m.mmsi == mmsi);
                if (model != null)
                {
                    listModel.Add(new ShipInfoListModel()
                    {
                        MMSI = model.mmsi,
                        Name = model.name,
                        Result = "配置正确"
                    });
                }
                else
                {
                    listModel.Add(new ShipInfoListModel()
                    {
                        Name = "找不到这艏船",
                        MMSI = mmsi,
                        Result = "MMSI错误"
                    });
                }
            }
            return Result(ResultType.Success, listModel);
        }


        #region  初始化更新所有船

        /// <summary>
        /// 更新所有船列表信息
        /// </summary>
        /// <returns></returns>
        public IServiceResult ModifyShipList()
        {
            var list = shipOp.QueryAllShips();
            foreach (var ship in list)
            {
                ShipXYModel model = (ShipXYModel)JsonHelper.DeserializeObject(ship.Value, typeof(ShipXYModel));
                ship.MMSI = model.mmsi;
                ship.Name = model.name;
                ship.Source = ShipInfo.AIS_CHUANXUN;
            }
            shipOp.ModifShips(list);
            return Result(ResultType.Success);
        }

        #endregion






    }
}